如何将图像和标记在TensorFlow |
您所在的位置:网站首页 › python tfds › 如何将图像和标记在TensorFlow |
问题描述
import tensorflow_datasets as tfds
train_ds = tfds.load('cifar100', split='train[:90%]').shuffle(1024).batch(32)
val_ds = tfds.load('cifar100', split='train[-10%:]').shuffle(1024).batch(32)
我想将train_ds和val_ds转换为类似的东西:x_train, y_train和x_val, y_val(x用于图像,y,标签). KERAS API使用火车和测试数据拆分(在Sklearn中也是如此),但是我 not 都想在这里使用任何测试数据. 我已经尝试过,但是它不起作用(我确实理解为什么这不起作用,但是我不知道我还能将培训数据转换为图像和标签): x_train = train_ds['image'] # TypeError: 'BatchDataset' object is not subscriptable 推荐答案我找到了一个更好的解决方案: train_ds, val_ds = tfds.load(name="cifar100", split=('train[:90%]','train[-10%:]'), batch_size=-1, as_supervised=True) x_train, y_train = tfds.as_numpy(train_data) x_val, y_val = tfds.as_numpy(val_data) 其他推荐答案不是最好的方法,我首先创建了列表以检查它们.我认为您想要类似的东西: train_ds = tfds.load('mnist', split='train[:90%]') train_examples_labels = tfds.as_numpy(train_ds) x_train = [] y_train = [] for features_labels in train_examples_labels: x_train.append(features_labels['image']) y_train.append(features_labels['label'])features_labels是这里的字典: features_labels.keys() dict_keys(['image', 'label'])可以将它们转换为numpy数组. x_train = np.array(x_train, dtype = 'float32') y_train = np.array(y_train, dtype = 'float32')本文地址:https://www.itbaoku.cn/post/2565639.html |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |